package test.jusfy.com.myapplication;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import win.smartown.android.library.tableLayout.TableAdapter;
import win.smartown.android.library.tableLayout.TableLayout;

public class MainActivity extends AppCompatActivity {
    //select name from sqlite_master where type='table' order by name;

    private List<SongTable> contentList;
    private TableLayout tableLayout;

    RadioGroup radioGroup;

    GridFragment gridFragment;
    ListFragment listFragment;

    FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup = (RadioGroup) findViewById(R.id.main_group);

        manager = getSupportFragmentManager();
        gridFragment = new GridFragment();
        listFragment = new ListFragment();

        //开启事务，添加默认显示的Fragment
        FragmentTransaction fragmentTransaction = manager.beginTransaction();
        fragmentTransaction.add(R.id.frame_container,gridFragment);
        //fragment添加到回退栈
        //fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkedId) {
                FragmentTransaction transaction= manager.beginTransaction();
                switch (checkedId){
                    case R.id.rb_grid:
                        transaction.replace(R.id.frame_container,gridFragment);
                        break;
                    case R.id.rb_list:
                        transaction.replace(R.id.frame_container,listFragment);
                        break;
                }
                transaction.commit();
            }
        });

       /* tableLayout = (TableLayout) findViewById(R.id.main_table);
        initContent();
        firstRowAsTitle();*/
        //firstColumnAsTitle();
    }


    private void initContent() {
        contentList = new ArrayList<>();
        contentList.add(new SongTable("id","loc","len","mp3loc","mp3len","songname","language","singer","search","M","L","searchsinger","midiname","mp3name","mtvname","dbpath","ensongname","ensingername","encomposer","enwriter","searchcomposer"));
        //contentList.add(new SongTable());
        SQLDataBaseManager sqlDataBaseManager = new SQLDataBaseManager();
        SQLiteDatabase db = sqlDataBaseManager.openDataBase(getApplicationContext());

        //查询数据库中的数据
        //Cursor cursor = db.rawQuery("select * from songtable where id = ?", new String[]{"52009"});
        Cursor cursor = db.rawQuery("select * from songtable",null);
        String name = null;
        String id = null;
        String loc = null;
        String len= null;
        String mp3loc= null;
        String mp3len= null;
        String songname= null;
        String language= null;
        String singer= null;

        String search;
        String M;
        String L;
        String searchsinger;
        String midiname;
        String mp3name;
        String mtvname;
        String dbpath;
        String ensongname;
        String ensingername;
        String encomposer;
        String enwriter;
        String searchcomposer;
        String RecNo =null;

        while (cursor.moveToNext()) {
            //name = cursor.getString(cursor.getColumnIndex("songname"))
             RecNo = cursor.getString(cursor.getColumnIndex("RecNo"));;
             id = cursor.getString(cursor.getColumnIndex("id"));
             loc = cursor.getString(cursor.getColumnIndex("loc"));
             len = cursor.getString(cursor.getColumnIndex("len"));
             mp3loc = cursor.getString(cursor.getColumnIndex("mp3loc"));
             mp3len= cursor.getString(cursor.getColumnIndex("mp3len"));
             songname = cursor.getString(cursor.getColumnIndex("songname"));
             language = cursor.getString(cursor.getColumnIndex("language"));
             singer = cursor.getString(cursor.getColumnIndex("singer"));


        }
        contentList.add(new SongTable(id,loc,len,mp3loc,mp3len,songname,language,singer,"search","M","L","searchsinger","midiname","mp3name","mtvname","dbpath","ensongname","ensingername","encomposer","enwriter","searchcomposer"));


        //contentList.add(new SongTable("id","loc","len","mp3loc","mp3len","songname","language","singer","search","M","L","searchsinger","midiname","mp3name","mtvname","dbpath","ensongname","ensingername","encomposer","enwriter","searchcomposer"));
        //contentList.add(new SongTable("id","loc","len","mp3loc","mp3len","songname","language","singer","search","M","L","searchsinger","midiname","mp3name","mtvname","dbpath","ensongname","ensingername","encomposer","enwriter","searchcomposer"));
        cursor.close();
    }

    private void firstColumnAsTitle() {
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return contentList.size();
            }

            @Override
            public String[] getColumnContent(int position) {
                return contentList.get(position).toArray();
            }
        });
    }





}
